import request from '@/utils/request'

/**
 * 数据备份与恢复API接口
 */

// 执行数据库备份
export function performBackup(backupName, backupType) {
  return request({
    url: '/api/backup/perform',
    method: 'post',
    params: { backupName, backupType }
  })
}

// 从上传文件恢复数据库
export function restoreFromUpload(file) {
  const formData = new FormData()
  formData.append('file', file)

  return request({
    url: '/api/backup/restore/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 从备份记录恢复数据库
export function restoreFromBackupRecord(backupId) {
  return request({
    url: `/api/backup/restore/${backupId}`,
    method: 'post'
  })
}

// 分页查询备份记录
export function getBackupPage(currentPage, pageSize, queryParams) {
  return request({
    url: `/api/backup/page/${currentPage}/${pageSize}`,
    method: 'get',
    params: queryParams
  })
}

// 根据操作人查询备份记录
export function getBackupsByOperator(operatorId) {
  return request({
    url: `/api/backup/operator/${operatorId}`,
    method: 'get'
  })
}

// 根据备份类型查询备份记录
export function getBackupsByType(backupType) {
  return request({
    url: `/api/backup/type/${backupType}`,
    method: 'get'
  })
}

// 获取最近的备份记录
export function getRecentBackups(limit = 10) {
  return request({
    url: '/api/backup/recent',
    method: 'get',
    params: { limit }
  })
}

// 删除备份记录和文件
export function deleteBackup(backupId) {
  return request({
    url: `/api/backup/${backupId}`,
    method: 'delete'
  })
}

// 批量删除备份记录和文件
export function batchDeleteBackups(backupIds) {
  return request({
    url: '/api/backup/batch',
    method: 'delete',
    data: backupIds
  })
}

// 下载备份文件
export function downloadBackupFile(backupId) {
  return request({
    url: `/api/backup/download/${backupId}`,
    method: 'get',
    responseType: 'blob'
  })
}

// 验证备份文件完整性
export function validateBackupFile(backupId) {
  return request({
    url: `/api/backup/validate/${backupId}`,
    method: 'get'
  })
}

// 获取备份统计信息
export function getBackupStatistics() {
  return request({
    url: '/api/backup/statistics',
    method: 'get'
  })
}

// 创建自动备份任务
export function createScheduledBackup(backupName, backupType, scheduleExpression) {
  return request({
    url: '/api/backup/schedule',
    method: 'post',
    params: { backupName, backupType, scheduleExpression }
  })
}

// 检查备份文件是否存在
export function checkBackupFileExists(backupId) {
  return request({
    url: `/api/backup/check/${backupId}`,
    method: 'get'
  })
}

// 获取备份文件大小
export function getBackupFileSize(backupId) {
  return request({
    url: `/api/backup/size/${backupId}`,
    method: 'get'
  })
}